<template>
  <!-- 门诊医生-中医处方 -->
  <a-spin :spinning="loading">
    <a-checkbox-group v-if="orderDataSource.length > 0" v-model:value="checkboxValue" class="flex flex-col">
      <div v-for="(orderItem, index) in orderDataSource" :key="orderItem.cfbh">
        <TCMList :order-index="index" :order-item="orderItem" class="relative">
          <a-checkbox :value="orderItem.cfbh" />
        </TCMList>
      </div>
    </a-checkbox-group>
    <a-empty v-if="orderDataSource.length === 0" class="mt-20" />
  </a-spin>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import TCMList from './TCMList'

const orderDataSource = ref([])
const checkboxValue = ref([])
const loading = ref(false)
const props = defineProps({
  jzbh: {
    type: String,
    default: ''
  }
})

//获取中药数据
const getData = async () => {
  orderDataSource.value = []
  checkboxValue.value = []
  loading.value = true
  const { code, data = [], msg } = await post('/outp/outpClinicalOrder/getOutpTCMHeadList', { jzbh: [props.jzbh] })
  loading.value = false
  if (code !== 0) return message.error(msg)
  if (data.length > 0) {
    const showData = []
    data.forEach((item) => {
      if (item.children.length > 0 && item.children?.[0].zxzt === 0) {
        showData.push(item)
      }
    })
    orderDataSource.value = showData
    checkboxValue.value = showData.map((list) => list.cfbh)
  } else {
    orderDataSource.value = []
    checkboxValue.value = []
  }
}
watchEffect(() => props.jzbh && getData())
defineExpose({ checkboxValue, orderDataSource, getData })
</script>
<style lang="less" scoped></style>
